package com.example.basic.demos.controller;


import com.example.basic.demos.annotation.SysLogAnnotation;
import com.example.basic.demos.annotation.TestAnnotation;
import com.example.basic.demos.common.response.CommonResult;
import com.example.basic.demos.entity.User;
import com.example.basic.demos.exception.ErrorEnum;
import com.example.basic.demos.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class IndexController {


    @Autowired
    private UserServiceImpl userService;

    @TestAnnotation
    @GetMapping("/allUser")
    public CommonResult<List<User>> getList() {
        List<User> userList = userService.getAll();
        return CommonResult.success(userList);
    }


//    @GetMapping("/oneUser/{id}")
//    public CommonResult<User> getOne(@PathVariable(value = "id") Integer id) {
//        /* @PathVariable(value = "id") Integer id, 注解作用是指定路径参数, 里面value和GetMapper{id}对应的 */
//        User user = userService.getOne(id);
//        return CommonResult.success(user);
//    }

    @GetMapping("/oneUser")
    public CommonResult<User> getOne(@RequestParam(value = "id") Integer id) {
        /* @RequestParam(value = "id") Integer id 注解作用是指定路径参数, 里面value和路径的?id对应的 */
        User user = userService.getOne(id);
        return CommonResult.success(user);
    }

    @PostMapping("/addUser")
    public CommonResult<String> addUser(@RequestBody User user) {
        Integer insert = userService.insert(user);
        return CommonResult.success("添加成功" + insert + "条");

    }

    @GetMapping("/error")
    public CommonResult<ErrorEnum> getErrorReq() {
        return CommonResult.error(ErrorEnum.INTERNAL_SERVER_ERROR);
    }


    @SysLogAnnotation(value = "陈江", desc = "cxk")
    @GetMapping("/testAnnotation")
    public CommonResult<String> testAnnotation(@RequestParam("name") String name, @RequestParam("age") Integer age) {
        return CommonResult.success("测试自定义注解接口" + name + "---" + age);
    }
}
